import tkinter as tk
from tkinter import ttk, messagebox, simpledialog
from PIL import Image, ImageTk
import os

class MemoApp(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("备忘录")
        self.geometry("400x400")
        self.resizable(False, False)

        # 创建笔记本卡片
        self.notebook = ttk.Notebook(self)
        self.notebook.pack(expand=True, fill="both")

        # 创建每日必做事项卡片
        self.daily_tasks_frame = tk.Frame(self.notebook)
        self.notebook.add(self.daily_tasks_frame, text="每日必做事项")
        self.daily_tasks_list = tk.Listbox(self.daily_tasks_frame)
        self.daily_tasks_list.pack(expand=True, fill="both")
        self.add_daily_task_button = tk.Button(self.daily_tasks_frame, text="添加任务", command=self.add_daily_task)
        self.add_daily_task_button.pack(side="left")
        self.delete_daily_task_button = tk.Button(self.daily_tasks_frame, text="删除任务",
                                                  command=self.delete_daily_task)
        self.delete_daily_task_button.pack(side="right")

        # 创建备忘录卡片
        self.memo_frame = tk.Frame(self.notebook)
        self.notebook.add(self.memo_frame, text="备忘栏")
        self.memo_list = tk.Listbox(self.memo_frame)
        self.memo_list.pack(expand=True, fill="both")
        self.add_memo_button = tk.Button(self.memo_frame, text="添加备忘", command=self.add_memo)
        self.add_memo_button.pack(side="left")
        self.delete_memo_button = tk.Button(self.memo_frame, text="删除备忘", command=self.delete_memo)
        self.delete_memo_button.pack(side="right")
        self.read_txt("每日必做.txt","备忘录.txt")

    def add_daily_task(self):
        task = tk.simpledialog.askstring("添加任务", "请输入每日必做事项：")
        if task:
            self.daily_tasks_list.insert(tk.END, task)
            with open("每日必做.txt", 'w', encoding='utf-8') as file:
                # 遍历Listbox中的所有项
                for item in self.daily_tasks_list.get(0, tk.END):
                    # 将每一项写入文件，并在其后添加一个换行符
                    file.write(item + '\n')

    def delete_daily_task(self):
        selected_index = self.daily_tasks_list.curselection()
        if selected_index:
            self.daily_tasks_list.delete(selected_index[0])
            with open("每日必做.txt", 'w', encoding='utf-8') as file:
                # 遍历Listbox中的所有项
                for item in self.daily_tasks_list.get(0, tk.END):
                    # 将每一项写入文件，并在其后添加一个换行符
                    file.write(item + '\n')

    def add_memo(self):
        memo = tk.simpledialog.askstring("添加备忘", "请输入备忘内容：")
        if memo:
            self.memo_list.insert(tk.END, memo)
            with open("备忘录.txt", 'w', encoding='utf-8') as file:
                # 遍历Listbox中的所有项
                for item in self.memo_list.get(0, tk.END):
                    # 将每一项写入文件，并在其后添加一个换行符
                    file.write(item + '\n')

    def delete_memo(self):
        selected_index = self.memo_list.curselection()
        if selected_index:
            self.memo_list.delete(selected_index[0])
            with open("备忘录.txt", 'w', encoding='utf-8') as file:
                # 遍历Listbox中的所有项
                for item in self.memo_list.get(0, tk.END):
                    # 将每一项写入文件，并在其后添加一个换行符
                    file.write(item + '\n')
    def read_txt(self,file_path1,file_path2):
        try:
            # 读取txt文件内容
            if not os.path.exists(file_path1):
                # 如果文件不存在，则创建它
                with open(file_path1, 'w') as file:
                    file.write('')
            with open(file_path1, "r") as file:  #"E:\每日必做.txt"
                #content = file.read()
                lines = file.readlines()
                for line in lines:
                    self.daily_tasks_list.insert(tk.END, line.strip())  # strip()
        except FileNotFoundError:
            messagebox.showwarning("文件未找到", "未找到memo.txt文件，请确保文件存在。")
        try:
            # 读取txt文件内容
            if not os.path.exists(file_path1):
                # 如果文件不存在，则创建它
                with open(file_path1, 'w') as file:
                    file.write('')
            with open(file_path2, "r") as file:  #"E:\每日必做.txt"
                #content = file.read()
                lines = file.readlines()
                for line in lines:
                    self.memo_list.insert(tk.END, line.strip())  # strip()

        except FileNotFoundError:
            messagebox.showwarning("文件未找到", "未找到memo.txt文件，请确保文件存在。")

    # 主程序入口

if __name__ == "__main__":
    app = MemoApp()
    app.mainloop()